package com.prnasia.cloud.spring.pcom.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.prnasia.cloud.spring.entity.StoryEntity;
import com.prnasia.cloud.spring.pcom.feign.StoryFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * Created by Wen Gao on 2019/4/8.
 */
@RestController
public class PcomController {

    @Autowired
    private EurekaClient discoveryClient;
    @Autowired
    private StoryFeignClient storyFeignClient;


    @GetMapping("getStoryFromAMP")
    public StoryEntity getStoryFromAMP(@RequestParam int id) {
        StoryEntity story = storyFeignClient.get(id);
        return story;
    }

    @PostMapping("addStoryToAMP")
    public StoryEntity addStoryToAMP(@RequestBody StoryEntity storyEntity) {
        System.out.println("add a story to amp through pcom: " + storyEntity);
        StoryEntity result = storyFeignClient.add(storyEntity);
        return result;
    }

    public void serviceUrl() {
        Set<String> allKnownRegions = discoveryClient.getAllKnownRegions();
        System.out.println("all known regions = " + allKnownRegions);
        List<InstanceInfo> ampInstances = discoveryClient.getInstancesById("amp");
        System.out.println("all amp instances = " + ampInstances);
    }

}
